package com.itshi.plic;

import com.itshi.pojo.Brand;
import com.itshi.until.JDBC;
import org.junit.Test;

import java.sql.*;
import java.util.ArrayList;

/**
 * @author げっしょく
 * @date 2021/8/20
 */
public class Test7 {
    @Test
    public void printAll() throws SQLException {
        Connection con = JDBC.getConnection();
        String sql = "select * from tb_brand";
        Statement sta = con.createStatement();
        ResultSet res = sta.executeQuery(sql);
        ArrayList<Brand> list = new ArrayList<>();
        while(res.next()){
            int id = res.getInt(1);
            String btand_name = res.getString(2);
            String commpany = res.getString(3);
            int ordered = res.getInt(4);
            String desceiption = res.getString(5);
            int status = res.getInt(6);
            list.add(new Brand(id,btand_name,commpany,ordered,desceiption,status));
        }
        for (Brand brand : list) {
            System.out.println(brand);
        }
    }

    @Test
    public void printsingle() throws SQLException {
        ArrayList<Brand> list = new ArrayList<>();
        Connection con = JDBC.getConnection();
        String sql = "select * from tb_brand where id=2";
        Statement sta = con.createStatement();
        ResultSet res = sta.executeQuery(sql);
        while(res.next()){
            int id = res.getInt(1);
            String btand_name = res.getString(2);
            String commpany = res.getString(3);
            int ordered = res.getInt(4);
            String desceiption = res.getString(5);
            int status = res.getInt(6);
            list.add(new Brand(id,btand_name,commpany,ordered,desceiption,status));
        }
        for (Brand brand : list) {
            System.out.println(brand);
        }
    }

    @Test
    public void insert() throws SQLException {
        Connection con = JDBC.getConnection();
        Statement sta = con.createStatement();
        String sql = "insert into tb_brand values(6,'牛马','牛马科技有限公司',200,'牛马公司致力于把所有人都变成牛马',1)";
        int i = sta.executeUpdate(sql);
        System.out.println(i);
    }



    @Test
    public void delete() throws SQLException {
        Connection con = JDBC.getConnection();
        String sql = "delete from tb_brand where id=6";
        Statement sta = con.createStatement();
        int i = sta.executeUpdate(sql);
        System.out.println(i);
    }


    @Test
    public void update() throws SQLException {
        Connection con = JDBC.getConnection();
        String sql = "update tb_brand set ordered=10 where id=1";
        Statement sta = con.createStatement();
        int i = sta.executeUpdate(sql);
        System.out.println(i);
    }
}

